#include "Cpu.h"
#include "pin_mux.h"
#include "dmaController1.h"
#include "clockMan1.h"
#include "lpuart1.h"
#include "lpit1.h"
#include "mcu.h"
#include "uart_drv.h"
#include "pwm_pal1.h"

#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#define BTN_GPIO            PTC
#define BTN_PORT            PORTC
#define BTN_PORT_IRQn   	PORTC_IRQn
#define BTN1_PIN            17U
#define BTN2_PIN            16U
#define BTN3_PIN            15U
#define BTN4_PIN            14U

uint8_t g_u8_pressed_btn1 = 0;
uint8_t g_u8_pressed_btn2 = 0;
uint8_t g_u8_pressed_btn3 = 0;
uint8_t g_u8_pressed_btn4 = 0;

uint8_t g_u8_RunEnabled_btn1;
uint8_t g_u8_RunEnabled_btn2;
uint8_t g_u8_RunEnabled_btn3;
uint8_t g_u8_RunEnabled_btn4;

uint16_t g_u16DelayTimeCnt;

static void LPIT0_ISR_Handle()
{

	if(LPIT_DRV_GetInterruptFlagTimerChannels(INST_LPIT1, 1))
	{
		LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1, 1<<0);
        Time_Tick();
		// Task_Update();
	}
}

void Time_Tick(void)  // 1ms delay
{
	if(g_u16DelayTimeCnt > 0)
	{
		g_u16DelayTimeCnt --;
	}
}

void MCU_DelayMS(uint16_t u16Time)
{
	g_u16DelayTimeCnt = u16Time;
	while(g_u16DelayTimeCnt>0);
}

void enableFPU(void)
{
	/* Enable FPU set both CPACR[CP11] and CPACR[CP10] to Full Access - 0b11 */
	S32_SCB->CPACR |= (S32_SCB_CPACR_CP10_MASK | S32_SCB_CPACR_CP11_MASK);
}


void Board_Init(void)
{
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                    g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

	/* Enable the floating point unit */
    enableFPU();

	/* Initialize PWM */
  	PWM_Init(&pwm_pal1Instance, &pwm_pal1Configs);

    Uart_Init();

	GPIO_Init();

    LPIT_DRV_Init(INST_LPIT1, &lpit1_InitConfig);
    /* Initialize LPIT channel 0 and configure it as a periodic counter
     * which is used to generate an interrupt every second.
     */
    LPIT_DRV_InitChannel(INST_LPIT1, 0, &lpit1_ChnConfig0);

    /* Install LPIT_ISR as LPIT interrupt handler */
    INT_SYS_InstallHandler(LPIT0_Ch0_IRQn, &LPIT0_ISR_Handle, (isr_t *)0);

    /* Start LPIT0 channel 0 counter */
    LPIT_DRV_StartTimerChannels(INST_LPIT1, (1 << 0));

}

void button_ISR(void)
{
	if (PINS_DRV_GetPortIntFlag(BTN_PORT) & (1 << BTN1_PIN))
	{
		if (g_u8_pressed_btn1 < 11)
		{
			if (g_u8_pressed_btn1 == 0)
			{
				g_u8_RunEnabled_btn1 = 1;
			}
			else if (g_u8_pressed_btn1 == 1)
			{
				g_u8_RunEnabled_btn1 = 2;
			}
			else if (g_u8_pressed_btn1 == 2)
			{
				g_u8_RunEnabled_btn1 = 3;
			}
			else if (g_u8_pressed_btn1 == 3)
			{
				g_u8_RunEnabled_btn1 = 4;
			}
			else if (g_u8_pressed_btn1 == 4)
			{
				g_u8_RunEnabled_btn1 = 5;
			}
			else if (g_u8_pressed_btn1 == 5)
			{
				g_u8_RunEnabled_btn1 = 6;
			}
			else if (g_u8_pressed_btn1 == 6)
			{
				g_u8_RunEnabled_btn1 = 7;
			}
			else if (g_u8_pressed_btn1 == 7)
			{
				g_u8_RunEnabled_btn1 = 8;
			}
			else if (g_u8_pressed_btn1 == 8)
			{
				g_u8_RunEnabled_btn1 = 9;
			}
			else if (g_u8_pressed_btn1 == 9)
			{
				g_u8_RunEnabled_btn1 = 10;
			}
			else if (g_u8_pressed_btn1 == 10)
			{
				g_u8_RunEnabled_btn1 = 11;
			}
			g_u8_pressed_btn1++;
		}
		PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN1_PIN);
	}
	
	if (PINS_DRV_GetPortIntFlag(BTN_PORT) & (1 << BTN2_PIN))
	{
		if (g_u8_pressed_btn2 < 2)
		{
			if (g_u8_pressed_btn2 == 0)
			{
				g_u8_RunEnabled_btn2 = 1;
			}
			else if (g_u8_pressed_btn2 == 1)
			{
				g_u8_RunEnabled_btn2 = 2;
			}
			g_u8_pressed_btn2++;
		}
		PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN2_PIN);
	}
	
	if (PINS_DRV_GetPortIntFlag(BTN_PORT) & (1 << BTN3_PIN))
	{
		if (g_u8_pressed_btn3 < 2)
		{
			if (g_u8_pressed_btn3 == 0)
			{
				g_u8_RunEnabled_btn3 = 1;
			}
			else if (g_u8_pressed_btn3 == 1)
			{
				g_u8_RunEnabled_btn3 = 2;
			}
			g_u8_pressed_btn3++;
		}
		PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN3_PIN);
	}
	
	if (PINS_DRV_GetPortIntFlag(BTN_PORT) & (1 << BTN4_PIN))
	{
		if (g_u8_pressed_btn4 < 2)
		{
			if (g_u8_pressed_btn4 == 0)
			{
				g_u8_RunEnabled_btn4 = 1;
			}
			else if (g_u8_pressed_btn4 == 1)
			{
				g_u8_RunEnabled_btn4 = 2;
			}
			g_u8_pressed_btn4++;
		}
		PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN4_PIN);
	}
}


void GPIO_Init(void)
{
    /* Install buttons ISR */
	INT_SYS_InstallHandler(BTN_PORT_IRQn, button_ISR, (isr_t *)NULL);

    /* Enable buttons interrupt */
    INT_SYS_EnableIRQ(BTN_PORT_IRQn);
}
